package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.BusinessException;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.ArticleDto;
import com.itheima.ydd.entity.Article;
import com.itheima.ydd.entity.YddCate;
import com.itheima.ydd.service.ArticleService;
import com.itheima.ydd.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("ydd_article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private YddCateService cateService;

    /**
     * 文章列表-分页查询
     * @param startTime
     * @param endTime
     * @param title
     * @param cateid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Long startTime,Long endTime,String title,Long cateid,long page,long limit){
        //分页构造器
        Page<Article> pageInfo = new Page<>(page,limit);
        Page<ArticleDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        //根据title进行模糊查询
        queryWrapper.like(title != "",Article::getTitle,title);
        //根据分类查询
        queryWrapper.eq(cateid != null,Article::getCateid,cateid);
        //根据时间查询
        queryWrapper.in(startTime != null,Article::getCreate_date,startTime);
        //根据创建时间降序排列
        queryWrapper.orderByDesc(Article::getCreate_date);
        Page<Article> articlePage = articleService.page(pageInfo, queryWrapper);
        //对象拷贝 因为两者类型不一样
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Article> records = pageInfo.getRecords();
        //item代表Setmeal对象
        List<ArticleDto> list = records.stream().map((item) -> {
            ArticleDto articleDto = new ArticleDto();
            //对象拷贝 因为其他参数都是null
            BeanUtils.copyProperties(item,articleDto);
            //分类id
            long itemCateid = item.getCateid();
            //根据分类id查询分类对象
            YddCate cate = cateService.getById(itemCateid);
            if(cate != null){
                //分类名称
                String categoryName = cate.getCatename();
                articleDto.setCatename(Collections.singletonList(categoryName));
            }
            return articleDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);

        PageResult pageResult = PageResult.init(articlePage.getTotal(), limit, page, dtoPage.getRecords());
        log.info("",pageResult);

        return ResultData.ok("",pageResult);
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @GetMapping("/del")
    private ResultData delete(Integer id){

        boolean flag = articleService.removeById(id);

        return ResultData.ok("",flag);
    }


    /**
     * 文章添加
     * @param article
     * @return
     */
    @PostMapping("add")
    public ResultData add(@RequestBody Article article){
        boolean save = articleService.save(article);
        return ResultData.ok("",save);
}


    /**
     * 根据id查询数据并返回
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id){
        Article article = articleService.getById(id);
        return ResultData.ok("",article);
    }

    /**
     * 修改文章
     * @param article
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Article article){
        boolean updateById = articleService.updateById(article);
        return ResultData.ok("",updateById);
    }
}
